package dto.api;

import models.application.*;
import models.pubobj.AniObjectState;
import org.apache.commons.lang3.StringUtils;
import play.libs.Json;
import services.AniPublicPropService;

import java.util.*;

/**
 * User: yeh
 * Date: 2/17/13
 * Time: 6:33 PM
 * For:  wilddragon
 */
public class ApiInputInfoDto {

    public Long id;

    public Long hostAppId;

    public Long stateId;

    public Long typeId;

    public String apiName;

    public String url;

    public Long methodId;

    public String description;

    public String params;

    public static String[] requiredRegisterFieldKeys = {
            "hostAppId",
            "typeId",
            "apiName",
            "url",
            "methodId",
            "params"
    };
    public static String[] requiredUpdateFieldKeys = {
            "id"
    };
    public static String[] requiredDeleteFieldKeys = {
            "id"
    };
    public ApiInputInfoDto(Map<String, String> apiInfoMap, AniObjectState state){
        this.id = StringUtils.isEmpty(apiInfoMap.get("id")) ? null : Long.parseLong(apiInfoMap.get("id"));
        this.hostAppId = Long.parseLong(apiInfoMap.get("hostAppId"));
        this.stateId = state.id;
        this.typeId = Long.parseLong(apiInfoMap.get("typeId"));
        this.apiName = apiInfoMap.get("apiName").trim();
        this.url = apiInfoMap.get("url").trim();
        this.methodId = Long.parseLong(apiInfoMap.get("methodId"));
        this.description = apiInfoMap.get("description").trim();
        this.params = apiInfoMap.get("params").trim();
    }
    public ApiInputInfoDto(Map<String, String> apiInfoMap){
        this.id = StringUtils.isEmpty(apiInfoMap.get("id")) ? null : Long.parseLong(apiInfoMap.get("id"));
        this.hostAppId = Long.parseLong(apiInfoMap.get("hostAppId"));
        this.typeId = Long.parseLong(apiInfoMap.get("typeId"));
        this.apiName = apiInfoMap.get("apiName").trim();
        this.url = apiInfoMap.get("url").trim();
        this.methodId = Long.parseLong(apiInfoMap.get("methodId"));
        this.description = apiInfoMap.get("description").trim();
        this.params = apiInfoMap.get("params").trim();
    }
    public ApiInputInfoDto(Long id, Long hostAppId, Long stateId, Long typeId, String apiName, String url, Long methodId, String description, String params) {
        this.id = id;
        this.hostAppId = hostAppId;
        this.stateId = stateId;
        this.typeId = typeId;
        this.apiName = apiName;
        this.url = url;
        this.methodId = methodId;
        this.description = description;
        this.params = params;
    }
    public ApiInputInfoDto(Long hostAppId, Long stateId, Long typeId, String apiName, String url, Long methodId, String description, String params) {
        this.hostAppId = hostAppId;
        this.stateId = stateId;
        this.typeId = typeId;
        this.apiName = apiName;
        this.url = url;
        this.methodId = methodId;
        this.description = description;
        this.params = params;
    }
    public ApiInputInfoDto(AniApi oneApiDao) {
        this.id = oneApiDao.id;
        this.hostAppId = oneApiDao.hostApp.id;
        this.stateId = oneApiDao.state.id;
        this.typeId = oneApiDao.type.id;
        this.apiName = oneApiDao.apiName;
        this.url = oneApiDao.url;
        this.methodId = oneApiDao.method.id;
        this.description = oneApiDao.description;
        this.params = Json.toJson(oneApiDao).asText();
    }

    public ApiInputInfoDto(Long id) {
        this.id = id;
    }

    public AniApi generateDao(AniApp hostApp){
        return new AniApi(
                hostApp,
                AniPublicPropService.getObjectStateWithIdOrName(this.stateId),
                AniPublicPropService.getObjectTypeWithIdOrName(this.typeId),
                this.apiName,
                this.url,
                AniPublicPropService.getHttpMethodWithIdOrName(this.methodId),
                this.description
        );
    }
    public List<AniApiParam> getParamsDao(AniApi generatedApi){
        return ApiParamDto.getApiParamsDao(
                ApiParamDto.generateFromJsonArrayStr(this.params, generatedApi),
                generatedApi
        );
    }
    public AniApp getHostAppDao(){
        return new AniApp(this.hostAppId);
    }
}
